home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / GETW.C < prev    next >
Encoding:
Text File  |  1991-08-05  |  904 b   |  35 lines

  1. /*  getw.c, from p. 444 of Turbo C Bible  */
  2. #include <stdio.h>
  3. main()
  4. {
  5.     int word1;
  6.     FILE *infile;
  7.     char filename[81];
  8.     printf("Enter name of a file to read from: ");
  9.     gets(filename);
  10.                 /*  Open the file for reading  */
  11.     if ((infile = fopen(filename, "rb")) == NULL)
  12.     {
  13.     printf("fopen failed.\n");
  14.     exit(0);
  15.     }
  16.                 /*  Get first word from file  */
  17.     if ((word1 = getw(infile)) == EOF)
  18.     {
  19.                 /*  Check if tere was a real error  */
  20.     if (feof(infile) != 0)
  21.     {
  22.         printf("File: %s at EOF\n", filename);
  23.         exit(0);
  24.     }
  25.     if (ferror(infile) != 0)
  26.     {
  27.         printf("File: %s Read error\n", filename);
  28.         exit(0);
  29.     }
  30.     }
  31.                 /*  Print out the first word in hexa-  */
  32.                 /*  decimal                            */
  33.     printf("The first word in file %s is: %X\n", filename, word1);
  34.     printf("Use 'TYPE %s' to confirm this.\n", filename);
  35. }